********************************************************************************
**  TITLE:      on2007_oes_contact_networks                                   **  
**  AUTHOR:	    Philippe Mongrain                                             **
**  DATA:       Ontario Election Study, 2007                                  **
**  DATE:	    October 2022 						                          **	
**  VERSION:    Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "on2007_oes_contact_networks", replace

* Open dataset

use "oes-P207-E-2007_F1.dta", clear


***************************
** INDEPENDENT VARIABLES **
***************************

* Response date

gen surveydate = mdy(INTMONTH,INTDAY,INTYEAR) // Interview date

format %tdMon_DD,_CCYY surveydate // Format interview date: DDMMYYYY

gen edate = 20071010 // Election date

gen electiondate = date(string(edate,"%8.0f"),"YMD") 

format %tdMon_DD,_CCYY electiondate // Format election date: DDMMYYYY

gen time = electiondate - surveydate // Number of days between interview and election

* Gender

gen male = RGENDER

recode male (5=0)

* Age

gen age = 2007 - DM1

* Education

gen education = DM2

recode education 0=.

replace education = education - 1

* Interest for election

gen interest = REFD3

* Party identification (provincial)

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if K1 == .

replace pidstatus_whole = 2 if K1 == 7

replace pidstatus_whole = 3 if K1 == 1 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if K1!=1 & K1!=. & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999
 
label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* News attentiveness

gen news = REFD1

* Name of the Conservative Premier of Ontario who won two elections in the 1990s - Correct answer (Mike Harris) = 1, Incorrect answer = 0

gen conservative2 = REFF1 // Conservative Premier of Ontario who won two elections in the 1990s

recode conservative2 (5=0)
recode conservative2 (.=0)

* Name of the current leader of the federal Liberal Party - Correct answer (Stéphane Dion) = 1, Incorrect answer = 0

gen lpcleader = REFF2 // LPC leader

recode lpcleader (5=0)
recode lpcleader (.=0)

* Level of government has the primary responsibility for health and education - Correct answer (Provincial) = 1, Incorrect answer = 0

gen responsibility = REFF3 // Level of government has the primary responsibility for health and education

recode responsibility 1=0 5=1 7=0 .=0

* Party promising to close Ontario's coal-fired power plants by 2014 - Correct answer (OLP) = 1, Incorrect answer = 0

gen coal = H1 // Party promising to close Ontario's coal-fired power plants by 2014

replace coal = 0 if H1!=1

* Party promising to consider funding for faith-based, religious schools - Correct answer (OPC) = 1, Incorrect answer = 0

gen religiousschools = H2 // Party promising to consider funding for faith-based, religious schools

replace religiousschools = 0 if H2!=2
recode religiousschools (2=1)

* Party that would lower the cost of electricity for bigindustries in Ontario - Correct answer (ONDP) = 1, Incorrect answer = 0

gen electricity = H3 // Party that would lower the cost of electricity for bigindustries in Ontario

replace electricity = 0 if H3!=3
recode electricity (3=1)

* Party wants to consider contracting out some medical procedures to privately-run medical clinics - Correct answer (OPC) = 1, Incorrect answer = 0

gen clinics = H4 // Party wants to consider contracting out some medical procedures to privately-run medical clinics

replace clinics = 0 if H4!=2
recode clinics (2=1)

* Political knowledge index

gen knowledge = conservative2 + lpcleader + responsibility + coal + religiousschools + electricity + clinics // Political knowledge index

* Perceived closeness (province)

gen closeness_whole = J2

recode closeness_whole 2=0

* Political discussion

gen discussion = REFD4

recode discussion 5=0 3=1 1=2


***********************************
** PROVINCIAL-LEVEL EXPECTATIONS **
***********************************

* Forecasts

gen forecast_whole = J1

* Identify correct forecasts

gen correct_whole_d = 0 if forecast_whole!=1 & forecast_whole!=.
replace correct_whole_d = 1 if forecast_whole == 1


**********
** SAVE **
**********

save "on2007_oes.dta", replace


********************************
** MAIN ANALYSES (PROVINCIAL) **
********************************

* Regression analysis and percentage of correct and incorrect forecasts

logistic correct_whole_d discussion pidstatus_whole interest news knowledge i.closeness_whole i.male age education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


*************************************
** SUMMARY STATISTICS (PROVINCIAL) **
*************************************

label variable correct_whole_d "Election winner (DV)"
label variable pidstatus_whole "Provincial PID"
label variable discussion "Discussion"
label variable interest "Interest"
label variable news "News attentiveness"
label variable knowledge "Political knowledge"
label variable closeness_whole "Perceived closeness"
label variable male "Gender (male = 1)"
label variable age "Age"
label variable education "Education"
label variable time "Time of interview"

estpost tabstat correct_whole_d discussion pidstatus_whole interest news knowledge closeness_whole male age education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "on2007_oes_summary_provincial.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2007 Ontario provincial election (OES), provincial level") replace

eststo clear

log close